<template>
  <div class="progress-bar" ref="progressBar" @click="progressClick">
    <div class="bar-inner">
      <div class="progress" ref="progress"></div>
      <div
        class="progress-btn-wrapper"
        ref="progressBtn"
        @touchstart.prevent="progressTouchStart"
        @touchmove.prevent="progressTouchMove"
        @touchend="progressTouchEnd"
      >
        <div class="progress-btn"></div>
      </div>
    </div>
  </div>
</template>
<script>
const progressBtnWidth = 16; //定义常量，进度条的小球的宽度是16
export default {
  props: {
    precent: {
      type: Number,
      default: 0
    }
  },
  created() {
    //想要不同的回调函数中，共享某些数据，可以将这些数据挂载到this对象
    this.touch = {};
  },
  methods: {
    // 开始拉动
    progressTouchStart(e) {
      this.touch.initiated = true; //标志位，表示已经初始化了
      this.touch.startX = e.touches[0].pageX;
      //把开始点击的时候，把这个时候进度条的偏移位置记录下来
      this.touch.left = this.$refs.progress.clientWidth;
    },
    // 拉动中
    progressTouchMove(e) {
      if (!this.touch.initiated) {
        return; //如果没有经过start直接move是需要return，停止事件的
      }
      const deltaX = e.touches[0].pageX - this.touch.startX;
      const offsetWidth = Math.min(
        this.$refs.progressBar.clientWidth - progressBtnWidth,
        Math.max(0, this.touch.left + deltaX)
      );
      //   改变滚动条的进度
      this._offset(offsetWidth);
    },
    // 拉动结束
    progressTouchEnd(e) {
      this.touch.initiated = false;
      this._triggerPercent();
    },
    _triggerPercent() {
      //进度条的长度=长-小球的宽度
      const barWidth = this.$refs.progressBar.clientWidth - progressBtnWidth;
      //播放的进度，拉取的播放进度
      const precent = this.$refs.progress.clientWidth / barWidth;
      //将值弹射出去，告诉父组件
      this.$emit("precentChange", precent);
    },
    //实现小球的偏移函数
    _offset(offsetWidth) {
      //显示播放的进度的进度条
      this.$refs.progress.style.width = `${offsetWidth}px`;
      //显示小球的偏移
      this.$refs.progressBtn.style[
        "transform"
      ] = `translate3d(${offsetWidth}px,0,0)`;
    },
    //点击进度条，改变进度条的进度
    progressClick(e) {
      //改进成这样：
      const rect = this.$refs.progressBar.getBoundingClientRect();//获取元素的左右上下的距离
      const offsetWidth = e.pageX - rect.left;//pageX是到页面的边的距离
      this._offset(offsetWidth);
      // 这里当我们点击progressBar的时候，e.offsetX获取不对
      //小球的偏移
      // this._offset(e.offsetX);
      //通知外层父组件，我们改变了多少进度条
      this._triggerPercent();
    }
  },
  watch: {
    precent(newPercent) {
      if (newPercent >= 0 && !this.touch.initiated) {
        //控制进度条拖动的时候，不要来回跳，就需要再加一个控制条件this.touch.initiated = false
        //进度条的长度
        const barWidth = this.$refs.progressBar.clientWidth - progressBtnWidth;
        //控制的圆圈的偏移的距离
        const offsetWidth = newPercent * barWidth;
        this._offset(offsetWidth);
      }
    }
  }
};
</script>
<style lang="stylus" scoped>
@import '../../common/stylus/variable.styl';

.progress-bar {
  height: 30px;

  .bar-inner {
    position: relative;
    top: 13px;
    height: 4px;
    background: rgba(0, 0, 0, 0.3);

    .progress {
      position: absolute;
      height: 100%;
      background: $color-theme;
    }

    .progress-btn-wrapper {
      position: absolute;
      left: -8px;
      top: -13px;
      width: 30px;
      height: 30px;

      .progress-btn {
        position: relative;
        top: 7px;
        left: 7px;
        box-sizing: border-box;
        width: 16px;
        height: 16px;
        border: 3px solid $color-text;
        border-radius: 50%;
        background: $color-theme;
      }
    }
  }
}
</style>